<template>
  <div style="display: flex;justify-content: center;margin-top: 150px">
    <el-card class="box-card" style="width: 550px;">
      <el-form :model="loginForm" :rules="rules" ref="loginInfo" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="20" :offset="1">
            <el-form-item label="用户名:" prop="userName">
              <el-input type="text" v-model="loginForm.userName" placeholder="用户名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20" :offset="1">
            <el-form-item label="密 码:" prop="passWord">
              <el-input type="text" v-model="loginForm.passWord" placeholder="密码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20" :offset="1">
            <el-button @click="login" type="primary" size="medium">登录</el-button>
            <el-button @click="register" size="medium">注册</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 
      cost*4-cost*2.5/100 = val
      cost*200
     -->
  </div>
</template>
<script>
// import { mapMutations } from 'vuex'
// import Logo from '~/components/Logo.vue'
import { login } from './env/service'
import { setStorageToken } from '@/config/localUtils'
export default {
  data() {
    return {
      loginForm: {
        userName: '',
        passWord: ''
      },
      // 校验规则
      rules: {
        userName: [
          { required: true, message: '输入用户名', trigger: 'blur' },
        ],
        passWord: [
          { required: true, message: '输入密码', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    //this.test();
  },
  created() {
    //this.test();
  },
  methods: {
    // 登录
    login() {
      this.$refs['loginInfo'].validate((valid) => {
        // 校验失败
        if (!valid) {
          return false;
        }
        let _this = this
        login(_this.loginForm, '登录', function (res) {
          if (res.status == 'SUCCESS') {
            const token = res.data;
            // 保存token到本地
            setStorageToken(token);
            _this.$message.success("登录成功");
            _this.$router.push('/homePage')
          } else {
            _this.$message.error(res.message);
          }
          // _this.$router.push('/home')
        })
      });
    },
    // 注册
    register() {
      this.$router.push('/register')
    }
  }
}
</script>
